package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.RoleDao;
import com.eric.husky.bean.Role;
import java.util.List;

@Service
public class RoleService {
    @Autowired
	private RoleDao roleDao;

	public Page<Role> list(Role role, Pageable page) {
		Example<Role> example = Example.of(role);
		return roleDao.findAll(example, page);
	}

	public Role get(Integer roleid) {
		return roleDao.findById(roleid).get();
	}

    public List<Role> findList() {
		return roleDao.findAll();
	}

	@Transactional
	public Role save(Role role) {
	    if(role.getRoleid()!=null){
	        Role roleOld = roleDao.findById(role.getRoleid()).orElse(null);
	        if(roleOld!=null){
	            UpdateUtil.copyNullProperties(role,roleOld);
                return roleDao.save(roleOld);
	        }
	    }
	    return roleDao.save(role);
	}

	@Transactional
	public void delete(Integer roleid) {
		roleDao.deleteById(roleid);
	}

}